﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFChatClient;
using WPFChatClient;

namespace WPFChatClient
{
    /// <summary>
    /// Interaction logic for ChatWindow.xaml
    /// </summary>


    public partial class ChatWindow : Window
    {
        private const String FormTitle = @"Group Chat: ";
        private const String StatusTitle = @"Connection Status: ";
        private MessageClient client;
        private String windowName;

        public ChatWindow(MainWindow mainWindow, String name)
        {
            this.client = mainWindow.getMessageClient();
            this.windowName = name;
            InitializeComponent();
            this.Title = "Chatting with: " + name;
        }

        /**
         * Adds a line of text to the message area.
         * Called using a delegate event in the MessageClient class.
         */
        public void client_updateMessageArea(string message)
        {
            Dispatcher.Invoke((Action)delegate
            {
                messageArea.Text += message + "\n";
            });
        }

        public String getName()
        {
            return windowName;
        }

        /**
         * Format and send the contents of the new message field to the server when the send button is clicked.
         */
        private void sendBtn_Click(object sender, EventArgs e)
        {
            transmitMessage(newMessageField.Text);
            newMessageField.Clear();
        }

        /**
         * Format and send the contents of the new message field to the server when the enter key is pressed.
         */
        private void newMessageField_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Convert.ToChar(Key.Enter)))
            {
                transmitMessage(newMessageField.Text);
            }
        }

        /**
         * 
         */
        private void transmitMessage(String newMessage)
        {
            if (newMessage.Length == 0)
                return;

            newMessage = client.formatGroupMessage(windowName, newMessage);
            client.sendThrottledMessage(newMessage);
        }

        /**
         * Clear the new message area when the enter key is released.
         */
        private void newMessageField_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                newMessageField.Clear();
            }
        }

        public void appendChatLine(String str)
        {
            Dispatcher.Invoke((Action)delegate
            {
                messageArea.AppendText(str + "\n");
            });
        }

        /**
         * Clear the text from the message area.
         */
        public void clearMessageScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispatcher.Invoke((Action)delegate
            {
                messageArea.Clear();
            });
        }

        private void messageArea_TextChanged(object sender, EventArgs e)
        {

        }


    }
}
